const { getList, getDetail, newBlog, updateBlog, delBlog } = require('../controller/blog');
const { SuccessModule, ErrorModule } = require('../module/resModule');
const handleBlogRouter = (req, res) => {
    const method = req.method;
    const id = req.query.id || '';
    if(method === 'GET' && req.path === '/api/blog/detail')  {
        const result = getDetail(id);
        return result.then(res => {
            return new SuccessModule(res);
        })
    }
    if(method === 'GET' && req.path === '/api/blog/list')  {
        const auther = req.query.auther || '';
        const keyword = req.query.keyword || '';
        const result = getList(auther, keyword);
        return result.then(listData => {
           return new SuccessModule(listData);
        })
    }
    if(method === 'POST' && req.path === '/api/blog/new')  {
        const postData = JSON.parse(req.body);
        const newData = newBlog(postData);
        return newData.then(res => {
            return new SuccessModule({
                id: res,
            });
        })
    }
    if(method === 'POST' && req.path === '/api/blog/update')  {
        const postData = JSON.parse(req.body);
        console.log(postData.auther);
        const update = updateBlog(id, postData);
        return update.then(res => {
            if(res) return new SuccessModule(res);
            else return new ErrorModule('更新失败');
         })
    }
    if(method === 'POST' && req.path === '/api/blog/del')  {
        const del = delBlog(id);
        return del.then(res => {
            if(res) return new SuccessModule(res);
            else return new ErrorModule('更新失败');
         })
    }
}
module.exports = handleBlogRouter;